package com.gitee.feizns.explore.data_structure.tree.bs.mid;

/**
 * 98. 验证二叉搜索树
 * @author feizns
 * @since 2020/1/7
 */
public class IsValidBST {

    public static void main(String[] args) {
        System.out.println(isValidBST(create()));
    }

    private static TreeNode create() {
        TreeNode ret = new TreeNode(3);
        ret.left = new TreeNode(1);
        ret.right = new TreeNode(4);
        ret.right.left = new TreeNode(2);
        return ret;
    }

    static int prev = Integer.MIN_VALUE;

    public static boolean isValidBST(TreeNode root) {
        if ( root != null ) {
            if ( !isValidBST(root.left) )
                return false;
            if ( prev >= root.val )
                return false;
            prev = root.val;
            if ( !isValidBST(root.right) )
                return false;
        }
        return true;
    }

}
